package com.group6.converter;

import java.util.Stack;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.text.Editable;
import android.text.TextWatcher;

public class Hexadecimal extends Activity implements TextWatcher{    
    EditText txtOctal;
    TextView txtDecimal,txtBinary,txtHexadecimal;
    
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hexadecimal);
        txtHexadecimal=(EditText)findViewById(R.id.txtHexadecimal);
        txtDecimal=(EditText)findViewById(R.id.txtDecimal);
        txtBinary=(EditText)findViewById(R.id.txtBinary);
        txtOctal=(EditText)findViewById(R.id.txtOctal);
        txtHexadecimal.addTextChangedListener(this);
        
        Button button = (Button) findViewById(R.id.Button03);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                back(v);
            }
        });
    }
    
    public void beforeTextChanged(CharSequence sequence,int start,int count,int after){
    }
    public void afterTextChanged(Editable editable){
    }
    public void onTextChanged(CharSequence sequence,int start,int before,int count){
        calculate(10,txtDecimal);        
        HexToBinary(2,txtBinary);       
        HexToOctal(8,txtOctal);   
    }
    public void calculate(int base,TextView txtView)
    {
        if(txtHexadecimal.getText().toString().trim().length()==0)
        {
            txtView.setText("");
            return;
        }
        String a = txtHexadecimal.getText().toString();
        try
        {
        	Integer output = Integer.parseInt(a, 16);
            StringBuffer buffer=new StringBuffer();
        {
                buffer.append(output.toString());
        }
        txtView.setText(buffer.toString());
    }
       catch (NumberFormatException e)
    {
        txtView.setText(e.getMessage());
    }
}
    
    public void HexToBinary(int base, TextView txtView)
    {
    	String a = txtBinary.getText().toString();
    	int number = Integer.parseInt(a, 16);
    	String bin = Integer.toBinaryString(number);
    	txtView.setText(bin.toString());
    	
    }
    
    public void HexToOctal(int base, TextView txtView)
    {
    	String a = txtBinary.getText().toString();
    	int number = Integer.parseInt(a, 16);
    	String oct = Integer.toOctalString(number);
    	txtView.setText(oct.toString());
    }
    
	public void back(View view){
    	Intent i = new Intent(Hexadecimal.this, Converter.class);
        startActivity(i);
    }
}